﻿using System.Collections.Generic;

namespace Skybot.Extension
{
    public static  class uintEx
    {

        /// <summary>
        /// 检索
        /// </summary>
        /// <param name="terminalcode"></param>
        /// <param name="terminal"></param>
        /// <returns></returns>
        public static int IndexOf(this uint terminalcode, List<uint> terminal)
        {
            var low = 0;
            var high = terminal.Count - 1;
            while (low <= high)
            {
                if (terminal[low] == terminalcode)
                    return low;
                if (terminal[high] == terminalcode)
                    return high;
                var middle = (low + high) % 2 == 0 ? (low + high) / 2 : (low + high) / 2 + 1;
                if (terminal[middle] == terminalcode)
                    return middle;
                if (terminalcode < terminal[middle]) //要比较的值比中间值小
                    high = middle - 1;
                else
                    low = middle + 1;
            }
            return -1;
        }

        /// <summary>
        /// 检索插入索引
        /// </summary>
        /// <param name="terminalcode"></param>
        /// <param name="terminal"></param>
        /// <returns></returns>
        public static int GetIndexOf(this uint terminalcode, IList<uint> terminal)
        {
            if (terminal.Count == 0) return 0;
            var low = 0;
            var high = terminal.Count - 1;
            while (low <= high)
            {
                if (terminalcode < terminal[low])
                    return low;
                if (terminalcode > terminal[high])
                    return high + 1;
                var middle = (low + high) % 2 == 0 ? (low + high) / 2 : (low + high) / 2 + 1;
                if (terminalcode < terminal[middle]) //要比较的值比中间值小
                    high = middle - 1;
                else
                    low = middle + 1;
            }
            return -1;
        }

        /// <summary>
        /// 经维度数据转换,主要在参数设置时用几字节进行处理
        /// </summary>
        /// <param name="input">double值</param>
        /// <returns></returns>
        public static byte[] DoubleToBin(this double input)
        {
            var lst = new List<byte>();
            var waitDo = input.ToString(System.Globalization.CultureInfo.InvariantCulture);
            var index = waitDo.IndexOf(char.Parse("."));
            for (int i = 0; i < 3 - index; i++)
            {
                waitDo = "0" + waitDo;
            }
            waitDo = waitDo.Replace(".", "");
            index = waitDo.Length;
            if (waitDo.Length > 10)
            {
                waitDo = waitDo.Substring(0, 10);
            }
            else
            {
                for (int i = 0; i < 10 - index; i++)
                {
                    waitDo = waitDo + "0";
                }
            }
            char[] result = waitDo.ToCharArray();
            for (int i = 0; i < result.Length; i = i + 2)
            {

                var tmp = int.Parse(result[i].ToString(System.Globalization.CultureInfo.InvariantCulture));
                tmp = tmp << 4;
                tmp = tmp | int.Parse(result[i + 1].ToString(System.Globalization.CultureInfo.InvariantCulture));
                lst.Add(System.BitConverter.GetBytes(tmp)[0]);
            }

            byte[] bytearr = lst.ToArray();
            lst.Clear();
            lst = null;

            return bytearr;
        }


    }
}
